package com.example.combwx.mvp.view.news;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.combwx.App;
import com.example.combwx.R;
import com.example.combwx.db.UserUtility;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseFragment;
import com.example.combwx.mvp.contract.LREContract;
import com.example.combwx.mvp.di.DaggerLREComponent;
import com.example.combwx.mvp.di.LREModules;
import com.example.combwx.mvp.model.HttpCode;
import com.example.combwx.mvp.model.entity.ChannelEntity;
import com.example.combwx.mvp.model.entity.TextBannerEntity;
import com.example.combwx.mvp.model.entity.UserEntity;
import com.example.combwx.mvp.presenter.LREPresenter;
import com.example.combwx.mvp.view.news.adapter.NewsVpAdapter;
import com.example.combwx.mvp.view.news.adapter.TextBannerAdapter;
import com.example.combwx.utils.LogUtils;
import com.example.combwx.utils.ToasetUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import constant.DownLoadBy;
import model.UpdateConfig;
import update.UpdateAppUtils;

public class NewsFragment extends BaseFragment<LREPresenter>
        implements LREContract.ILREViwe,
        OnBannerListener<TextBannerEntity.Values>,View.OnClickListener{


    private LinearLayout newsLinlat;
    private ImageView newsIcon;
    private ImageView newsAudioImg;
    private ImageView newsSearchIcon;
    private Banner newsTextBanner;
    private ImageView newsMoreImg;
    private SlidingTabLayout newsTab;
    private ViewPager newsVp;
    private NewsVpAdapter newsVpAdapter;
    private List<Fragment> fragments;
    private TextBannerAdapter textBannerAdapter;

//    @Override
//    public View addStatusView() {
//        return f(R.id.news_linlat);
//    }
//
//    @Override
//    public int addColor() {
//        return Color.WHITE;
//    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_news;
    }


    @Override
    public void initView() {
        UpdateConfig config=new UpdateConfig(true,true,false,false,false
                , "","apk.1.1"
                , DownLoadBy.APP
                ,false,true
                ,0
                ,false,true,
                "",2);
        UpdateAppUtils.getInstance().apkUrl("upDataEntity.getUrl()")
                .updateTitle("更新版本")
                .updateContent("更新新版本？要不然你走开~~~")
                .updateConfig(config)
                .update();
        newsLinlat = (LinearLayout) f(R.id.news_linlat);
        newsIcon = (ImageView) f(R.id.news_icon);
        newsAudioImg = (ImageView) f(R.id.news_audio_img);
        newsSearchIcon = (ImageView) f(R.id.news_search_icon);
        newsTextBanner = (Banner) f(R.id.news_text_banner);
        newsMoreImg = (ImageView) f(R.id.news_more_img);
        newsMoreImg.setOnClickListener(this::onClick);
        newsTab = (SlidingTabLayout) f(R.id.news_tab);
        newsVp = (ViewPager) f(R.id.news_vp);
    }

    @Override
    public void initData() {
        fragments = new ArrayList<>();
        //获取顶部文字banner数据源
        try {
            List<UserEntity> users = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list();
            long user_id = 0;
            boolean flag = users.size() == 0;
            user_id = users.get(0).user_id;
            HashMap<String, Object> bannerMap = new HashMap<>();
            bannerMap.put("code", HttpCode.TEXTBANNERCODE);
            HashMap<String, Object> channelMap = new HashMap<>();
            channelMap.put("code",HttpCode.USERCHANNELCODE);
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE)!= PackageManager.PERMISSION_GRANTED){
                return;
            }
            channelMap.put("imie",PhoneUtils.getIMEI());
            channelMap.put("userId",user_id);
            p.requestAll(bannerMap,channelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectNewsFragment(this);
    }


    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerEntity){
            TextBannerEntity textBannerEntity = (TextBannerEntity) entity;
            LogUtils.show("textBannerEntity:"+textBannerEntity.values);
            if (textBannerEntity.getValues() == null){
                ToasetUtils.showMsg("文字广告数据丢失");
                return;
            }
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            newsTextBanner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setOnBannerListener(this);
        }else if (entity instanceof ChannelEntity){
            ChannelEntity channelEntity = (ChannelEntity) entity;
            if (channelEntity.getValues() == null){
                ToasetUtils.showMsg("无频道数据");
                return;
            }
            for (int i = 0; i <channelEntity.getValues().size() ; i++) {
                fragments.add(new ContentNewsFragment(channelEntity.getValues().get(i).channel_id));
            }
            newsVpAdapter = new NewsVpAdapter(getFragmentManager(),channelEntity.getValues(),fragments);
            newsVp.setAdapter(newsVpAdapter);
            newsTab.setViewPager(newsVp);
        }

    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void OnBannerClick(TextBannerEntity.Values data, int position) {
        ToasetUtils.showMsg("点击广告:"+data.news_title);
    }

    @Override
    public void onStart() {
        super.onStart();
        newsTextBanner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        newsTextBanner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        newsTextBanner.destroy();
    }

    @Override
    public void onClick(View view) {
        startPage(ChannelActivity.class);
    }
}
